package com.example.javamaday.leetcode;

public class LeetCodeTest1702 {

    public static void main(String[] args) {

    }

    public String maximumBinaryString(String binary) {
        char[] source = binary.toCharArray();
        if (source.length <= 1) {
            return binary;
        }
        int n = binary.length();
        int right_one = 0;
        boolean hasZero = false;
        for (int i = 0; i < source.length; i++) {
            if (source[i] == '0') {
                hasZero = true;
            } else {
                if (hasZero) {
                    right_one++;
                }
            }
        }
        // 如果全是1，不需要任何转换
        if (!hasZero) {
            return binary;
        }
        for (int i = n - 1; i >= 0; i--) {
            if (n-1-right_one == i) {
                source[i] = '0';
            } else {
                source[i] = '1';
            }
        }
        return new String(source);
    }
}
